﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections;
using System.Linq;
using System.Text;
using TerraExplorerX;
using Utilities;
using Entity;
using Bussiness;

namespace UI
{
    public class SLHelper
    {
        private ISGWorld61 sgworld=new SGWorld61();
        public double _Speed = 3;
        public double _TurnSpeed = 300;
        public double _ModelScale = 1;
        public string _ModelURL = null;
        public ActionCode _ActionCode = ActionCode.AC_FOLLOWCOCKPIT;
        public SLHelper()
        {
            
        }
        //创建线对象
        public void CreatePolyline(Collection<ERouteNode> nodes,int slgroupId,string layerName,bool flyto)
        {
            string tMsg = String.Empty;
            double[] cVerticesArray = null;
            IGeometry cPolylineGeometry = null;
            ITerrainPolyline61 cPolyline = null;
            try
            {
                cVerticesArray = new double[nodes.Count * 3];
                for (int i = 0; i < nodes.Count; i++)
                {
                    cVerticesArray.SetValue(nodes[i].Longitude, i * 3);
                    cVerticesArray.SetValue(nodes[i].Latitude, i * 3 + 1);
                    cVerticesArray.SetValue(nodes[i].Altitude, i * 3 + 2);
                }

                cPolylineGeometry = sgworld.Creator.GeometryCreator.CreateLineStringGeometry(cVerticesArray);
                uint nLineColor = 0xFF00FF00; // Abgr value -> solid green
                cPolyline = sgworld.Creator.CreatePolyline(cPolylineGeometry, nLineColor, AltitudeTypeCode.ATC_TERRAIN_RELATIVE, slgroupId, layerName);
                cPolyline.LineStyle.Width = 0.5;

                IPosition61 cFlyToPos = cPolyline.Position.Copy();
                cFlyToPos.Pitch = -89.0; // Set camera to look downward on polygon
                if (flyto)
                {
                    sgworld.Navigate.FlyTo(cFlyToPos, ActionCode.AC_FLYTO);
                }

            }
            catch (Exception ex)
            {
                tMsg = String.Format("GeometryPolygon_Click Exception: {0}", ex.Message);
                //MessageBox.Show(tMsg);
            }
        }

        public void AddTextLabel(int slGroupId,string name, IPosition61 pos, bool flyTo)
        {
            ITerrainLabel61 textLabel = sgworld.Creator.CreateTextLabel(pos, name, null, slGroupId, name);
            if (flyTo)
            {
                sgworld.Navigate.FlyTo(textLabel);
            }
        }
        public void RunByRoute(int crCode,int groupID)
        {
            SysConfigsManager cfm = new SysConfigsManager();
            Collection<ERouteNode> nodes = new Collection<ERouteNode>();
            BestRoutesManager br = new BestRoutesManager();

            //int lastGID = Convert.ToInt32(cfm.GetParaByName("LastGroupID").ToString());
            nodes = br.BestRouteGetByCode(crCode, groupID);

            RunByNodeCollection(nodes);
        }

        public void RunByNodeCollection(Collection<ERouteNode> nodes)
        {
            ITerrainDynamicObject61 DynamicObj = sgworld.Creator.CreateDynamicObject(0, DynamicMotionStyle.MOTION_GROUND_VEHICLE, DynamicObjectType.DYNAMIC_3D_MODEL,
                _ModelURL, _ModelScale, AltitudeTypeCode.ATC_TERRAIN_RELATIVE, 0, "");
            DynamicObj.CircularRoute = false;
            DynamicObj.Action.Code = _ActionCode;//关键属性
            DynamicObj.TreeItem.ShowInViewerTree = false;//关键属性
            for (int i = 0; i < nodes.Count; i++)
            {
                IRouteWaypoint61 waypoint = sgworld.Creator.CreateRouteWaypoint(nodes[i].Longitude, nodes[i].Latitude, nodes[i].Altitude, _Speed, 0, 0, 0, 0, 0, "poi");
                DynamicObj.Waypoints.AddWaypoint(waypoint);
            }

            ////DynamicObj.Visibility.Show = false;//控制车模型是否显示
            DynamicObj.Position.Distance = 10;//设置viewing distance
            DynamicObj.TurnSpeed = _TurnSpeed;//重要，这样才能按正常的路线飞行

            sgworld.Navigate.FlyTo(DynamicObj, ActionCode.AC_FOLLOWCOCKPIT);//FlyTo中设置ActionCode，这样一开始飞行，就能按AC_FOLLOWCOCKPIT来飞行
            //br.UpdateRouteStatus(crCode, lastGID);
        }
        public void AddImgLabel(int gourpId,EClassRoom cr, string str, string imgPath, bool flyto)
        {
            if (cr != null)
            {
                IPosition61 pos = sgworld.Creator.CreatePosition(cr.Lon, cr.Lat, cr.Alt, AltitudeTypeCode.ATC_TERRAIN_RELATIVE);
                ILabelStyle61 style = sgworld.Creator.CreateLabelStyle();
                style.TextColor = sgworld.Creator.CreateColor(255, 255, 255, 255);
                style.ShowTextBehavior = ShowTextOptions.SHOW_TEXT_ON_HOVER;
                style.FontName = "黑体";
                ITerrainLabel61 imgLabel = sgworld.Creator.CreateLabel(pos, str,
                    imgPath, style, gourpId, Description: cr.CRCode.ToString());
                if (flyto)
                {
                    sgworld.Navigate.FlyTo(imgLabel);
                }
            }
        }
        public int AddGroup(string name)
        {
            sgworld.ProjectTree.CreateGroup(name, 0);
            int itemID = sgworld.ProjectTree.FindItem(name);
            return itemID;
        }
    }
}
